home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 January / macformat-020.iso / Shareware City / Developers / apps.to.go / pbClock / DoEvent.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-18  |  5.6 KB  |  232 lines  |  [TEXT/MPS ]

  1. /*
  2. ** Apple Macintosh Developer Technical Support
  3. **
  4. ** File:        DoEvent.c
  5. ** Written by:    Eric Soldan
  6. **
  7. ** Copyright © 1990-1993 Apple Computer, Inc.
  8. ** All rights reserved.
  9. */
  10.  
  11. /* You may incorporate this sample code into your applications without
  12. ** restriction, though the sample code has been provided "AS IS" and the
  13. ** responsibility for its operation is 100% yours.  However, what you are
  14. ** not permitted to do is to redistribute the source as "DSC Sample Code"
  15. ** after having made changes. If you're going to re-distribute the source,
  16. ** we require that you make it clear in the source that the code was
  17. ** descended from Apple Sample Code, but that you've made changes. */
  18.  
  19.  
  20.  
  21. /*****************************************************************************/
  22.  
  23.  
  24.  
  25. #include "App.h"            /* Get the application includes/typedefs, etc.    */
  26. #include "App.defs.h"        /* Get various application definitions.            */
  27. #include "App.protos.h"        /* Get the prototypes for application.            */
  28.  
  29. #ifndef __CTLHANDLER__
  30. #include "CtlHandler.h"
  31. #endif
  32.  
  33. #ifndef __DESK__
  34. #include <Desk.h>
  35. #endif
  36.  
  37. #ifndef __DISKINIT__
  38. #include <DiskInit.h>
  39. #endif
  40.  
  41. #ifndef __ERRORS__
  42. #include <Errors.h>
  43. #endif
  44.  
  45. #ifndef __MENUS__
  46. #include <Menus.h>
  47. #endif
  48.  
  49. #ifndef __TEXTEDITCONTROL__
  50. #include "TextEditControl.h"
  51. #endif
  52.  
  53. #ifndef __TEXTSERVICES__
  54. #include "TextServices.h"
  55. #endif
  56.  
  57. #ifndef __TOOLUTILS__
  58. #include <ToolUtils.h>
  59. #endif
  60.  
  61. #ifndef __UTILITIES__
  62. #include "Utilities.h"
  63. #endif
  64.  
  65.  
  66.  
  67. /*****************************************************************************/
  68.  
  69.  
  70.  
  71. extern Cursor    *gCursorPtr;    /* See the file Window.c for comments about this global. */
  72.  
  73.  
  74.  
  75. /*****************************************************************************/
  76. /*****************************************************************************/
  77.  
  78.  
  79.  
  80. /* Do the right thing for an event.  Determine what kind of event it is, and
  81. ** call the appropriate routines. */
  82.  
  83. #pragma segment Main
  84. void    DoEvent(EventRecord *event)
  85. {
  86.     WindowPtr    window, oldPort;
  87.     Point        pt;
  88.     OSErr        err;
  89.     TEHandle    teHndl;
  90.     Rect        rct;
  91.  
  92.     switch(event->what) {
  93.  
  94.         case nullEvent:
  95.             DoIdleTasks(event);
  96.             break;
  97.  
  98.         case mouseDown:
  99.             DoMouseDown(event);
  100.             break;
  101.  
  102.         case autoKey:
  103.         case keyDown:                    /* Check for menukey equivalents. */
  104.             DoKeyDown(event);
  105.             break;
  106.  
  107.         case activateEvt:
  108.             gCursorPtr = nil;            /* Force recalculation of the cursor. */
  109.             DoActivate((WindowPtr)event->message);
  110.             if (TSMTEAvailable()) TSMEvent(event);
  111.             break;
  112.  
  113.         case updateEvt:
  114.             DoUpdate((WindowPtr)event->message);
  115.             break;
  116.  
  117.         case kHighLevelEvent:
  118.             gCursorPtr = nil;            /* Force recalculation of the cursor. */
  119.             DoHighLevelEvent(event);
  120.             break;
  121.  
  122.         case osEvt:
  123.             gCursorPtr = nil;            /* Force recalculation of the cursor. */
  124.             switch ((event->message >> 24) & 0xFF) {
  125.                     /* Must logical and with 0xFF to get only low byte. */
  126.                     /* High byte of message. */
  127.  
  128.                 case mouseMovedMessage:
  129.                     DoIdleTasks(event);
  130.                     break;
  131.  
  132.                 case suspendResumeMessage:
  133.                         /* Suspend/resume is also an activate/deactivate. */
  134.                     gInBackground = !((event->message) & resumeFlag);
  135.                     CTEConvertClipboard((event->message) & convertClipboardFlag, !gInBackground);
  136.                     DoActivate(FrontWindow());
  137.                     HiliteWindows();
  138.                     break;
  139.             }
  140.             break;
  141.  
  142.         case diskEvt:
  143.             gCursorPtr = nil;            /* Force recalculation of the cursor. */
  144.             if (HiWord(event->message) != noErr) {
  145.                 SetPt(&pt, kDILeft, kDITop);
  146.                 err = DIBadMount(pt, event->message);
  147.             }
  148.             break;        /* It is not a bad idea to at least call DIBadMount
  149.                         ** in response to a diskEvt, so that the user can
  150.                         ** format a floppy. */
  151.     }
  152.  
  153.     DoCursor();
  154.     DoAdjustMenus();
  155.  
  156.     window = CTETargetInfo(&teHndl, &rct);
  157.     if (window) {
  158.         if (rct.left < -8192) {
  159.             GetPort(&oldPort);
  160.             SetPort(window);
  161.             SetOrigin(-16384, 0);
  162.             CTEIdle();
  163.             SetOrigin(0, 0);
  164.             SetPort(oldPort);
  165.         }
  166.         else {
  167.             BeginContent(window);
  168.             CTEIdle();
  169.             EndContent(window);
  170.         }
  171.     }
  172. }
  173.  
  174.  
  175.  
  176. /*****************************************************************************/
  177.  
  178.  
  179.  
  180. /* •• Called by DTS.Lib..framework. •• */
  181.  
  182. /* This is called when a window is activated or deactivated. */
  183.  
  184. #pragma segment Main
  185. void    DoActivate(WindowPtr window)
  186. {
  187.     RgnHandle    rgn, oldClip;
  188.     Point        pt;
  189.  
  190.     NotifyCancel();
  191.  
  192.     if (IsAppWindow(window)) {
  193.  
  194.         SetPort(window);
  195.         DoCtlActivate(window);
  196.         DoDrawFrame(window, true);            /* Redraw frame for new activate state. */
  197.  
  198.         CopyRgn(((WindowPeek)window)->contRgn, rgn = NewRgn());
  199.         DiffRgn(rgn, ((WindowPeek)window)->updateRgn, rgn);
  200.             /* Don't draw any part that's already destined to draw due to an update event.
  201.             ** This prevents part of an exposed window from drawing twice, and thus avoids
  202.             ** flickering. */
  203.  
  204.         BeginContent(window);
  205.  
  206.         pt.h = pt.v = 0;
  207.         GlobalToLocal(&pt);
  208.         OffsetRgn(rgn, pt.h, pt.v);
  209.             /* This offset may seem wrong, since the origin may not be 0,0.  It is actually correct,
  210.             ** since at the time of the GlobalToLocal, the port's origin was set to the content origin.
  211.             ** A clearer way to look at this is in two steps:
  212.             ** 1) With the port's origin at 0,0, do a GlobalToLocal on 0,0, and then offset the new clip
  213.             **    by that amount.
  214.             ** 2) Once the port's origin is set correctly, the new clip needs to be offset again, based
  215.             **    on the origin value.  (The clipRgn travels with the origin, unlike the visRgn.)
  216.             ** The above is what automatically happens when you do the GlobalToLocal of 0,0 on the
  217.             ** correct origin.  Offsets are additive.  One step -- no muss, no fuss. */
  218.  
  219.         GetClip(oldClip = NewRgn());
  220.         SetClip(rgn);
  221.         DoDrawControls(window, true);        /* Redraw content scrollbars for new activate state. */
  222.         SetClip(oldClip);
  223.         DisposeRgn(oldClip);
  224.         DisposeRgn(rgn);
  225.  
  226.         EndContent(window);
  227.     }
  228. }
  229.  
  230.  
  231.  
  232.